home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / BdfFontFile.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  2.8 KB  |  127 lines

  1. #
  2. # THIS IS WORK IN PROGRESS
  3. #
  4. # The Python Imaging Library
  5. # $Id: BdfFontFile.py,v 1.1.1.2 1999/01/13 09:40:15 sjoerd Exp $
  6. #
  7. # bitmap distribution font file parser
  8. #
  9. # history:
  10. # 96-05-16 fl    created (as bdf2pil)
  11. # 97-08-25 fl   converted to FontFile driver
  12. #
  13. # Copyright (c) Secret Labs AB 1997-98.
  14. # Copyright (c) Fredrik Lundh 1997.
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18.  
  19. import Image
  20. import FontFile
  21.  
  22. import string
  23.  
  24. # --------------------------------------------------------------------
  25. # parse X Bitmap Distribution Format (BDF)
  26. # --------------------------------------------------------------------
  27.  
  28. bdf_slant = {
  29.    "R": "Roman",
  30.    "I": "Italic",
  31.    "O": "Oblique",
  32.    "RI": "Reverse Italic",
  33.    "RO": "Reverse Oblique",
  34.    "OT": "Other"
  35. }
  36.  
  37. bdf_spacing = {
  38.     "P": "Proportional",
  39.     "M": "Monospaced",
  40.     "C": "Cell"
  41. }
  42.  
  43. def bdf_char(f):
  44.  
  45.     # skip to STARTCHAR
  46.     while 1:
  47.     s = f.readline()
  48.     if not s:
  49.         return None
  50.     if s[:9] == "STARTCHAR":
  51.         break
  52.     id = string.strip(s[9:])
  53.  
  54.     # load symbol properties
  55.     props = {}
  56.     while 1:
  57.     s = f.readline()
  58.     if not s or s[:6] == "BITMAP":
  59.         break
  60.     i = string.find(s, " ")
  61.     props[s[:i]] = s[i+1:-1]
  62.  
  63.     # load bitmap
  64.     bitmap = []
  65.     while 1:
  66.     s = f.readline()
  67.     if not s or s[:7] == "ENDCHAR":
  68.         break
  69.     bitmap.append(s[:-1])
  70.     bitmap = string.join(bitmap, "")
  71.  
  72.     [x, y, l, d] = map(string.atoi, string.split(props["BBX"]))
  73.     [dx, dy] = map(string.atoi, string.split(props["DWIDTH"]))
  74.  
  75.     bbox = (dx, dy), (l, -d-y, x+l, -d), (0, 0, x, y)
  76.  
  77.     im = Image.fromstring("1", (x, y), bitmap, "hex", "1")
  78.  
  79.     return id, string.atoi(props["ENCODING"]), bbox, im
  80.  
  81.  
  82. class BdfFontFile(FontFile.FontFile):
  83.  
  84.     def __init__(self, fp):
  85.  
  86.         s = fp.readline()
  87.         if s[:13] != "STARTFONT 2.1":
  88.             raise SyntaxError, "not a valid BDF file"
  89.  
  90.         FontFile.FontFile.__init__(self)
  91.  
  92.         props = {}
  93.         comments = []
  94.  
  95.         while 1:
  96.             s = fp.readline()
  97.             if not s or s[:13] == "ENDPROPERTIES":
  98.                 break
  99.             i = string.find(s, " ")
  100.             props[s[:i]] = s[i+1:-1]
  101.             if s[:i] in ["COMMENT", "COPYRIGHT"]:
  102.                 if string.find(s, "LogicalFontDescription") < 0:
  103.                     comments.append(s[i+1:-1])
  104.  
  105.         font = string.split(props["FONT"], "-")
  106.  
  107.         font[4] = bdf_slant[font[4]]
  108.         font[11] = bdf_spacing[font[11]]
  109.  
  110.         ascent = string.atoi(props["FONT_ASCENT"])
  111.         descent = string.atoi(props["FONT_DESCENT"])
  112.  
  113.         fontname = string.join(font[1:], ";")
  114.  
  115.         # print "#", fontname
  116.         # for i in comments:
  117.         #    print "#", i
  118.  
  119.         font = []
  120.         while 1:
  121.             c = bdf_char(fp)
  122.             if not c:
  123.                 break
  124.             id, ch, (xy, dst, src), im = c
  125.             if ch >= 0:
  126.                 self.glyph[ch] = xy, dst, src, im
  127.